var thisUrl;

function initUrl() {
	/* chrome.tabs.getSelected(null,function(tab){  
		thisUrl = tab.url; 	
	}); */
	chrome.tabs.query({
		active: true,
		currentWindow: true
	}, tabs => {
		thisUrl = getRootUrl(tabs[0].url);
	//	thisUrl = tabs[0].url;
		initIsInsideHtml();
		// use `url` here inside the callback because it's asynchronous!
	});
}


var isInsideEle = document.getElementById('isInside');
isInsideEle.onclick = function() {
	// alert(isInsideEle.checked);
	// var theurl = document.location.host;
	var map = {};
	var mapstr = window.localStorage.lmUrlMap;
	if (mapstr) {
		// map = eval('(' + mapstr + ')');
		map = JSON.parse(mapstr);
	}
	
	if(isInsideEle.checked){
		delete map[thisUrl];
		// 恢复正常图标
		chrome.browserAction.setIcon({
			path
		});
	}else{
		var trueval = true;
		map[thisUrl] = trueval;
		//设置灰色图标
		grey_img(path,function(imgObj){
					// 设置灰色图标
					chrome.browserAction.setIcon({
						imageData: imgObj.ctx.getImageData(0,0,128,128)
					});
				});
	}
	// map[thisUrl] = !isInsideEle.checked;
	window.localStorage.lmUrlMap = JSON.stringify(map);
	reload();
};

! function() {
	initUrl();
}();

function initIsInsideHtml() {
	// console.info(window.localStorage.isInside);
	// if(window.localStorage.lmUrlMap == 'true'){
	// 	isInsideEle.checked = true;
	// }
	// var theurl = document.location.host;
	var map = {};
	var mapstr = window.localStorage.lmUrlMap;
	if (mapstr) {
		// map = eval('(' + mapstr + ')');
		map = JSON.parse(mapstr);
	}
	isInsideEle.checked = !map[thisUrl];
	if(!isInsideEle.checked){
		//设置灰色图标
		grey_img(path,function(imgObj){
					// 设置灰色图标
					chrome.browserAction.setIcon({
						imageData: imgObj.ctx.getImageData(0,0,128,128)
					});
				});
	}else{
		// 恢复正常图标
		chrome.browserAction.setIcon({
			path
		});
	}
}


function reload(){
	chrome.tabs.query({
		active: true,
		currentWindow: true
	}, tabs => {
		chrome.tabs.sendMessage(tabs[0].id,{type:'refresh'},function(response){
			
		});
	});
}
